package com.flower8.controller;
import com.flower8.model.SysAdmin;
import com.flower8.model.UserToken;
import com.flower8.service.SysConfigService;
import com.flower8.service.TokenManager;
import com.flower8.utils.IpUtil;
import com.flower8.utils.SpringUtil;
import com.flower8.utils.StringUtil;
import com.flower8.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登陆相关接口
 * @author tanglin
 */
@RestController
@RequestMapping
public class LoginController extends BaseController {

	@Autowired
	private ServerProperties serverProperties;
	@Autowired
	private SysConfigService sysConfigService;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	/**
	 * 账号登录
	 * @param userName
	 * @param password
	 */
	@PostMapping("/sys/login")
	public void login(HttpServletRequest request, String userName, String password) {
		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName, password);
		SecurityUtils.getSubject().login(usernamePasswordToken);
		// 设置shiro的session过期时间
		long timeout = serverProperties.getServlet().getSession().getTimeout().toMillis();
		SecurityUtils.getSubject().getSession().setTimeout(timeout);
		String sessionId = SecurityUtils.getSubject().getSession().getId().toString();
		SysAdmin sysAdmin = UserUtil.getCurrentUser();
		UserToken userToken = new UserToken();
		userToken.setUserId(sysAdmin.getId().toString());
		userToken.setAccount(userName);
		userToken.setTimeout(timeout);
		String host = IpUtil.getIpAddr(request);
		userToken.setHost(host);
		TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
		tokenManager.saveSessionId(sessionId, userToken);
	}

	/**
	 * 当前登录用户
	 * @return
	 */
	@PostMapping("/sys/getLoginInfo")
	public Object getLoginInfo() {
		SysAdmin sysAdmin = UserUtil.getCurrentUser();
		SysAdmin sysAdminRet = new SysAdmin();
		sysAdminRet.setId(sysAdmin.getId());
		sysAdminRet.setAccount(sysAdmin.getAccount());
		sysAdminRet.setNickName(sysAdmin.getNickName());
		sysAdminRet.setStatus(sysAdmin.getStatus());

		Map<String, Object> data = new HashMap<>();
		data.put("userInfo", sysAdminRet);
		String sessionId = SecurityUtils.getSubject().getSession().getId().toString();
		data.put("sessionId", sessionId);

		return responseResultForSuccess(data);
	}

	/**
	 * 后台管理系统配置信息
	 * @param request
	 * @return
	 */
	@PostMapping("/sys/getAdminSysInfo")
	public Object getAdminSysInfo(HttpServletRequest request){
		Map<String, Object> res = new HashMap<>();
		try {
			List<Map> resMap =  sysConfigService.getAdminSysInfo();
			res.put("info", resMap);
		}catch (Exception e){
			e.printStackTrace();
		}
		return responseResultForSuccess(res, "");
	}

}
